﻿using KipLink.App_Code.Util;
using KipLink.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace KipLink.Controllers
{
    public class LinkApiController : ApiController
    {
        private kipLinkOra db = new kipLinkOra();
        // GET api/LinkApi
        public IEnumerable<LINK> GetLink()
        {
            return db.LINKs.AsEnumerable();
        }
        // GET api/LinkApi/id
        public IEnumerable<LINK> GetLink(int id)
        {
            return db.LINKs.SqlQuery("Select * FROM Link WHERE Id=" + id).AsEnumerable();
        }
        // GET api/Link/mamun
        public IEnumerable<LINK> GetLinkByuserName(string name)
        {
            return db.LINKs.SqlQuery("Select * FROM Link WHERE UserName='"+name+"'").AsEnumerable();
        }
        // POST api/Link
        public HttpResponseMessage PostLink(LINK link)
        {
            if (ModelState.IsValid)
            {
                db.LINKs.Add(link);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, link);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { UserName = link.USERNAME }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
        // PUT api/Link/5
        public HttpResponseMessage PutLink(LINK link)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
            db.Entry(link).State = EntityState.Modified;
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }
        // DELETE api/Link/5
        public HttpResponseMessage DeleteLink(int id)
        {
            LINK link = db.LINKs.Find(id);
            if (link == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.LINKs.Remove(link);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            return Request.CreateResponse(HttpStatusCode.OK, link);
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
	}
}